package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.UserBean;
import com.project.mapper.IUserMapper;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户业务接口实现类
 */
@Service
@Transactional
public class UserServiceImpl implements IUserService {
    @Autowired
    private IUserMapper userMapper;
    @Override
    public UserBean login(String userName, String pwd) {
        //查询对象
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        //用户名和密码都匹配，登陆才成功
        qw.eq("u_name",userName);
        qw.eq("u_pwd",pwd);

        //登陆成功，返回登陆对象，不成功，返回null
        return userMapper.selectOne(qw);
    }

    @Override
    public void add(UserBean user) {
        userMapper.insert(user);
    }

    @Override
    public UserBean findById(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public void updateGrade(Integer id, String grade) {
        UserBean user = userMapper.selectById(id);
        user.setGrade(grade);
        userMapper.updateById(user);
    }

    @Override
    public void del(Integer id) {
        userMapper.deleteById(id);
    }

    @Override
    public void updatePwd(Integer id, String newPwd) {
        UserBean user = userMapper.selectById(id);
        user.setPwd(newPwd);
        userMapper.updateById(user);
    }

    @Override
    public IPage<UserBean> cutAll(Integer pageNO) {
        return userMapper.selectPage(
                new Page<>(pageNO,3),//分页配置对象
                null //分页查询条件
        );
    }
}
